import { Component, OnInit, AfterViewInit } from '@angular/core';
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';

@Component({
    selector: 'app-translate01',
    templateUrl: './translate01.component.html',
    styleUrls: [
        './translate01.component.css'
    ]
})
export class Translate01Component implements OnInit, AfterViewInit {

    private langs: string[];
    private currentLang: string = 'zh-CN';

    constructor(private router: Router, private translate: TranslateService) {
        // 添加语言支持
        this.translate.addLangs(['zh-CN', 'en']);
        // 设置默认语言，一般在无法匹配时使用
        this.translate.setDefaultLang('zh-CN');
        //获取当前浏览器环境的语言比如en、 zh
        const browserLang = this.translate.getBrowserLang();
        this.translate.use(browserLang.match(/zh-CN|en/) ? browserLang : 'zh-CN');
        //this.initTranslateConfig();
    }

    ngOnInit() {
    }

    ngAfterViewInit() {
        /* this.langs = this.translate.getLangs();
        if (this.langs != undefined && this.langs.length > 0)
            this.changeLang(null, this.langs[0]);
        else
            this.changeLang(null, 'zh-CN'); */
    }
    /**@description 初始化语言配置 */
    initTranslateConfig() {
    }
    /**@description 下拉框切换语言 */
    changeLang($event, lang) {
        console.log(lang);
        this.currentLang = lang;
        this.translate.use(lang);
    }

    /**@description 按钮获取当前语言 */
    toggleLang() {
        // 获取语言风格，相当于更详细的语言类型，比如zh-CN/zh-TW/en-US
        //this.currentLang = this.translate.getBrowserCultureLang();
        this.translate.get(["hello", "welcome"])
            .subscribe(res => {
                console.log(res);
            });

    }
}